home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Source / Decision Cube / mxtables.pas < prev   
Pascal/Delphi Source File  |  1999-08-11  |  7KB  |  199 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Borland Delphi Visual Component Library         }
  4. {                                                       }
  5. {       Copyright (c) 1997,99 Inprise Corporation       }
  6. {                                                       }
  7. {*******************************************************}
  8.  
  9. unit mxtables;
  10.  
  11. interface
  12.  
  13. uses
  14.   Windows, SysUtils, Graphics, Classes, Controls, Db, DBCommon, Bde,
  15.   dbTables, mxcommon, mxqparse, mxqedcom, mxarrays;
  16.   
  17. type
  18.   TQueryDim = class(TDimensionItem)
  19.   public
  20.     property Active;
  21.   end;
  22.  
  23.   TQueryDimClass = class of TQueryDim;
  24.  
  25.   TQueryDims = class(TDimensionItems)
  26.   private
  27.   protected
  28.     function GetQueryDim(Index: Integer): TQueryDim;
  29.     procedure SetQueryDim(Index: Integer; Value: TQueryDim);
  30.     constructor Create(Owner: TPersistent; ItemClass: TQueryDimClass);
  31.   public
  32.     function Add: TQueryDim;
  33.     property Items[Index: Integer]: TQueryDim read GetQueryDim write SetQueryDim; default;
  34.   end;
  35.  
  36.   TDecisionQuery = class(TQuery)
  37.   end;
  38.  
  39.   function BuildDimensionMap(myQuery: TXtabQuery; Map: TQueryDims; SQLString: string): TQueryError;
  40.   function BuildMasterDimensionMap(myQuery: TXtabQuery; Map: TQueryDims): TQueryError;
  41.   function AddDimensionItem(myQuery: TXtabQuery; Map: TQueryDims; index: integer; Name: string): TQueryError;
  42.   function RemoveDimensionItem(myQuery: TXtabQuery; Map: TQueryDims; index: integer): TQueryError;
  43.  
  44. implementation
  45.  
  46. uses
  47.   mxstore, dbConsts, BDEConst, mxconsts;
  48.  
  49. {
  50.   Build Dimension Map builds a correct map for the input Dataset
  51.   The strategy for field naming differs depending on the type of dataset.
  52.   for DecisionQuery:  Parses the masterSQL, assumies that the user intends
  53.   all fields in query only if the query is a legal crosstab.
  54.   for Queries:  Checks against the SQL string, assuming all are InQuery;
  55.   for other Datasets: Assumes the Fields array is available
  56.  
  57.   This function attempts to preserve existing Mapitems.
  58.   Field matching works as follows:
  59.  
  60.   Query, Decision Query projected fields have FieldName set to the value
  61.   of their Projector.outputname at creation time, and it is never changed
  62.   Matching is done against this name.
  63.   Decision query derived fields have Map.DerivedFrom>=0 on creation.
  64.   Fieldname set to Fieldname of the field on which they are based, and
  65.   DimensionType set to the derived agg type.  The combination of type
  66.   and the derived fieldname is used for matching.
  67.   Name is never used for matching.
  68. }
  69.  
  70. function BuildMasterDimensionMap(myQuery: TXtabQuery; Map: TQueryDims): TQueryError;
  71. var
  72.   i: integer;
  73.   fQuery: TXtabQuery;
  74.   SQLString: string;
  75. begin
  76.   fQuery := TXtabQuery.Create;
  77.   fQuery.canDelete := false;
  78.   try
  79.     fQuery.DBHandle := myQuery.DBHandle;
  80.     SQLString := myQuery.SQLString;
  81.     if not CheckIfEmptyQuery(SQLString) then
  82.     begin
  83.       fQuery.SQLString := SQLString;
  84.       fQuery.DeleteProjectors;
  85.       SQLString := fQuery.SQLString;
  86.     end;
  87.     AddToQuerySelect(SQLString, '*') ;
  88.     Result := BuildDimensionMap(fQuery, Map, SQLString);
  89.     for i:= 0 to Map.count-1 do
  90.       Map[i].active := false;
  91.   finally
  92.     fQuery.free;
  93.   end;
  94. end;
  95.  
  96. function BuildDimensionMap(myQuery: TXTabQuery; Map: TQueryDims; SQLString: string): TQueryError;
  97. var
  98.   j: Integer;
  99.   NewItem: TQueryDim;
  100. begin
  101.   Result := tqeNotInitialized;
  102.   if not assigned(Map) then Exit;
  103.   {
  104.     Set up a data base, TXTabQuery and get an
  105.     initial projector list
  106.  
  107.     if the query string is empty, add a constant just to get it to parse
  108.     without an error.  Then delete it.
  109.   }
  110.   if CheckifEmptyQUery(SQLString) then
  111.   begin
  112.     AddToQuerySelect(SQLString, '1') ;
  113.     myQuery.SQLString := SQLString;
  114.     for j := myQuery.NProjectors-1 downto 0 do
  115.       myQuery.DeleteProjector(j);
  116.   end
  117.   else
  118.     myQuery.SQLString := SQLString;
  119.   {
  120.     Pass back as a final result whether or not the query is legal
  121.     Result := myQuery.isLegal;
  122.  
  123.     Now rebuild the dimension map.  Position items in the same order as the
  124.     Query.
  125.  
  126.     First go through all the actually projected fields and see which
  127.     ones match against the Map passed in.  Fields may be out of position,
  128.     but cannot have different (fieldname,dimensiontype) combinations.
  129.  
  130.     First do the fields which are actually in the query (never derived fields)
  131.   }
  132.   Map.clear;
  133.   for j := 0 to myQuery.NProjectors-1 do
  134.   begin
  135.     NewItem := Map.Add;
  136.     if (NewItem.index <> j) then NewItem.Index := j;
  137.     Map[j].Name := myQuery.Projector[j].OutputName;
  138.     Map[j].FieldName := myQuery.Projector[j].CompareName;
  139.     Map[j].BaseName := myQUery.Projector[j].BaseName;
  140.     Map[j].DimensionType := MyQuery.Projector[j].ProjType;
  141.     Map[j].FieldType := myQuery.Projector[j].FieldType;
  142.     Map[j].active := True;
  143.   end;
  144.   MyQuery.FixUpGroupBys;
  145.   Result := myQuery.isLegal;
  146. end;
  147.  
  148. {
  149.   Change the SQL query which will be run without changing the master query
  150.   This function is used by the Cube manager to allow the running
  151.   SQL to be altered from the Master
  152. }
  153.  
  154. function AddDimensionItem(myQuery: TXtabQuery; Map: TQueryDims; index: integer; Name: string): TQueryError;
  155. var
  156.   proj: integer;
  157. begin
  158.   myQuery.canDelete := false;
  159.   proj := myQuery.addNewItem(Map[index].basename,map[index].DimensionType, index, true, Name);
  160.   Map[index].FieldName := myQuery.Projector[proj].CompareName;
  161.   Map[index].Name := myQuery.Projector[proj].OutputName;
  162.   Map[index].BaseName := myQuery.Projector[proj].BaseName;
  163.   Map[index].DimensionType := MyQuery.Projector[proj].ProjType;
  164.   Map[index].active := True;
  165.   Result := tqeOK;
  166. end;
  167.  
  168. function RemoveDimensionItem(myQuery: TXtabQuery; Map: TQueryDims; index: integer): TQueryError;
  169. begin
  170.   myQuery.canDelete := false;
  171.   Map[index].active := false;
  172.   myQuery.DeleteProjector(index);
  173.   Result := myQuery.isLegal;
  174. end;
  175.  
  176.   { DataCube Collection Definition }
  177.  
  178. constructor TQueryDims.Create(Owner: TPersistent; ItemClass: TQueryDimClass);
  179. begin
  180.   inherited Create(Owner, ItemClass);
  181. end;
  182.  
  183. function TQueryDims.GetQueryDim(Index: Integer): TQueryDim;
  184. begin
  185.   Result := TQueryDim(inherited Items[Index]);
  186. end;
  187.  
  188. procedure TQueryDims.SetQueryDim(Index: Integer; Value: TQueryDim);
  189. begin
  190.   Items[Index].Assign(Value);
  191. end;
  192.  
  193. function TQueryDims.Add: TQueryDim;
  194. begin
  195.   Result := TQueryDim(inherited Add);
  196. end;
  197.  
  198. end.
  199.